
package com.sx.core.utils;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.time.DateUtils;

import java.text.ParseException;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

/**
 * Created by lihao on 16/4/5.
 */
public class ProviderUtils {
    public static final char UNDERLINE='_';
    public static String underlineToCamel(String param){
        if (param==null||"".equals(param.trim())){
            return "";
        }
        StringBuilder sb=new StringBuilder(param);
        Matcher mc= Pattern.compile("_").matcher(param);
        int i=0;
        while (mc.find()){
            int position=mc.end()-(i++);
            sb.replace(position-1,position+1,sb.substring(position,position+1).toUpperCase());
        }
        return sb.toString();
    }
    public static String camelToUnderline(String param){
        if (param==null||"".equals(param.trim())){
            return "";
        }
        int len=param.length();
        StringBuilder sb=new StringBuilder(len);
        for (int i = 0; i < len; i++) {
            char c=param.charAt(i);
            if (Character.isUpperCase(c)){
                sb.append(UNDERLINE);
                sb.append(Character.toLowerCase(c));
            }else{
                sb.append(c);
            }
        }
        return sb.toString();
    }
    public static String fillStr(String src,int index,String value){
        if(StringUtils.isEmpty(value)){
            return src;
        }
        if(index<=0){
            index=1;
        }
        char[] chars=src.toCharArray();
        chars[index-1]=value.charAt(0);
        return new String(chars);
    }
    //start从1开始
    public static String replaceStr(String src,int start,int end,String value){
        if(StringUtils.isEmpty(value)){
            return src;
        }
        String st=src.substring(0,start-1);
        String en=src.substring(end,src.length());
        return st+value+en;
    }



    public static String fillDefaultStr(String src,String value){
        char[] chars=src.toCharArray();
        for(int i=0;i<chars.length;i++){
            chars[i]=value.charAt(0);
        }
        return new String(chars);
    }
    // 获得本周一0点时间
    public static Date getTimesWeekmorning() {
        Calendar cal = Calendar.getInstance();
        cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONDAY), cal.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
        cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
        return  cal.getTime();
    }

    // 获得本周日24点时间
    public  static Date getTimesWeeknight() {
        Calendar cal = Calendar.getInstance();
        cal.setTime(getTimesWeekmorning());
        cal.add(Calendar.DAY_OF_WEEK, 7);
        return cal.getTime();
    }
    private static String[] formats=new String[]{"yyyy-MM-dd","yyyy-MM-dd HH:mm:ss","yyyy-MM-dd'T'HH:mm:ss","yyyyMMdd"};
    public static Date parseDate(String date){
        try {
            return DateUtils.parseDate(date,formats);
        } catch (ParseException e) {
            return null;
        }
    }
    public static String msk(String str,int start_len,int end_len){
        if(str==null)return null;
        int len=str.length();
        if(len<start_len+end_len)return null;
        return str.substring(0,start_len)+"****"+str.substring(len-end_len);
    }
    public static String[]  deduplicate(String[] arrays){
        return Arrays.stream(arrays).map(o->o).collect(Collectors.toSet()).stream().toArray(String[]::new);
    }



    public static void main(String[] args){

        String[] argss=new String[]{"1","1","2"};
        String[] argssss = (String[])deduplicate(argss);


        System.out.println(argssss.length);


    }
}
